//
//  BBHistoryManger.m
//  BlockBrowser
//
//  Created by apple on 2018/4/8.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHistoryManger.h"

@implementation BBHistoryManger
- (NSArray<BBScanHistoryModel *> *)collectionArray{
    NSMutableArray * array = [NSMutableArray new];
    NSArray * existarray = [[NSUserDefaults standardUserDefaults] objectForKey:collectionHistoryListKey];
    for (NSString * json in existarray) {
        NSData * data = [json dataUsingEncoding:NSUTF8StringEncoding];
        if (json.length) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            BBScanHistoryModel *model = [BBScanHistoryModel mj_objectWithKeyValues:dic];
            [array addObject:model];
        }
    }
    return array;
}
- (NSArray<BBScanHistoryModel *> *)scanArray{
    NSMutableArray * array = [NSMutableArray new];
    NSArray * existarray = [[NSUserDefaults standardUserDefaults] objectForKey:scanHistoryListKey];
    for (NSString * json in existarray) {
        NSData * data = [json dataUsingEncoding:NSUTF8StringEncoding];
        if (json.length) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            BBScanHistoryModel *model = [BBScanHistoryModel mj_objectWithKeyValues:dic];
            [array addObject:model];
        }
    }
    return array;
}
+ (BBHistoryManger *)manger{
    static BBHistoryManger * shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [BBHistoryManger new];
    });
    return shared;
}
- (void)addScanModel:(BBScanHistoryModel *)model{
    NSArray * array = [[NSUserDefaults standardUserDefaults] objectForKey:scanHistoryListKey];
    NSMutableArray * currentArray = [array mutableCopy];
    if (!currentArray) {
        currentArray = [NSMutableArray new];
        
    }
    [currentArray insertObject:model.mj_JSONString atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:currentArray forKey:scanHistoryListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (void)addColletionModel:(BBScanHistoryModel *)model{
    NSArray * array = [[NSUserDefaults standardUserDefaults] objectForKey:collectionHistoryListKey];
    NSMutableArray * currentArray = [array mutableCopy];
    if (!currentArray) {
        currentArray = [NSMutableArray new];
        
    }
    [currentArray insertObject:model.mj_JSONString atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:currentArray forKey:collectionHistoryListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)removeAllScanArray{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:scanHistoryListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)removeAllCollectionArray{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:collectionHistoryListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

